#include "Prize.h"
#include "SB.h"
#include "InputManager.h"
#include "hRandom.h"
#include "LevelManager.h"

Prize::Prize(std::string _sPrize, Vector2 _vPos):RenderableEntity(_sPrize, _vPos)
{
    m_vDirection = Vector2(SB::SCREEN_WIDTH * 0.5f, SB::SCREEN_HEIGHT * 0.25f) - _vPos;
    m_vDirection.normalize();
    m_vDirection *= 50.0f;
    m_vDirection2 = m_vDirection;
    m_vDirection.y += hRandom::getRandom(-10.0f, 10.0f);
    m_vDirection2.x += hRandom::getRandom(-10.0f, 10.0f);
    
    m_vDirection *= 0.1f;
    m_vDirection2 *= 0.1f;
    
    Vector2 vDirectionNormalized = m_vDirection;
    vDirectionNormalized.normalize();
    setPosition(getPosition() + Vector2(vDirectionNormalized * 10.0f));
    
    m_fLifetime = 0.0f;
    
    setCentered();
    setScale(Vector2(0.0f,0.0f));
}

void Prize::update()
{
    m_fLifetime += SB::dt;
    
    // move the prize
    float fSpeed = 4.0f;
    if (m_fLifetime < 0.4f)
    {
        fSpeed = 30.0f;
    }
    m_vDirection += m_vDirection2 * SB::dt * 3.0f;
    setPosition(getPosition() + m_vDirection * SB::dt * fSpeed);
    
    // scale the prize
    if (m_fLifetime < 0.3f)
    {
        float fScale = (m_fLifetime/0.3f) * 1.2f;
        setScale(Vector2(fScale, fScale));
    }
    else if (m_fLifetime < 0.4f)
    {
        float fScale = 1 + (((1 -(m_fLifetime - 0.3f)/ 0.1f)) * 0.1f);
        setScale(Vector2(fScale, fScale));
    }
    else
    {
        setScale(Vector2(1.0f, 1.0f));
    }
    setScale( getScale() * 0.8f );
}

bool Prize::playerTookIt()
{
    // check if player took it
    if (InputManager::instance().hasJustReleased())
    {
        Vector2 vTrazo = InputManager::instance().getPosition() - InputManager::instance().getLastJustPressedPosition();
        if (vTrazo.length() < 20.0f)
        {
            Vector2 vPressed = InputManager::instance().getPosition() * 2;
            return (vPressed - m_vPosition).length() < 100.0f;
        }
    }
    return false;
}